package cn.psvmc.cxzapi.util;

import cn.psvmc.cxzapi.config.ConfigKey;
import com.alibaba.fastjson2.JSON;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.ocr_api20210707.AsyncClient;
import com.aliyun.sdk.service.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.sdk.service.ocr_api20210707.models.RecognizeIdcardResponse;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;

/**
 * @Description 身份证识别
 * @Author ywx
 * Date 2024/8/10 12:38
 **/
@Slf4j
public class RecognizeIdcardUtil {

    /**
     * @Description 身份证识别
     * @Date 2024/8/10 13:48
     * @Author YWX
     * @Param []
     * @Return java.lang.String
     **/
    public static String recognizeIdCard(String filepath) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                                                                                      .accessKeyId(ConfigKey.accessKeyId)
                                                                                      .accessKeySecret(ConfigKey.accessKeySecret)
                                                                                      //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                                                                                      .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                                        .region("cn-hangzhou") // Region ID
                                        .credentialsProvider(provider)
                                        .overrideConfiguration(
                                                ClientOverrideConfiguration.create()
                                                                           .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
                                                //.setConnectTimeout(Duration.ofSeconds(30))
                                        )
                                        .build();

        // Parameter settings for API request
        InputStream bodyStream = new FileInputStream(filepath);
        RecognizeIdcardRequest recognizeIdcardRequest = RecognizeIdcardRequest.builder()
                                                                              .body(bodyStream)
                                                                              // Request-level configuration rewrite, can set Http request parameters, etc.
                                                                              // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                                                                              .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<RecognizeIdcardResponse> response = client.recognizeIdcard(recognizeIdcardRequest);
        // Synchronously get the return value of the API request
        RecognizeIdcardResponse resp = response.get();
        client.close();
        return JSON.toJSONString(resp);
    }

    /**
     * @Description 身份证识别 https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeidcard?spm=a2c4g.11186623.0.i1
     * @Date 2024/8/10 22:52
     * @Author YWX
     * @Param [file]
     * @Return java.lang.String
     **/
    public static String recognizeIdCard(MultipartFile file) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                                                                                      .accessKeyId(ConfigKey.accessKeyId)
                                                                                      .accessKeySecret(ConfigKey.accessKeySecret)
                                                                                      //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                                                                                      .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                                        .region("cn-hangzhou") // Region ID
                                        .credentialsProvider(provider)
                                        .overrideConfiguration(
                                                ClientOverrideConfiguration.create()
                                                                           .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
                                                //.setConnectTimeout(Duration.ofSeconds(30))
                                        )
                                        .build();

        // Parameter settings for API request
        InputStream bodyStream = file.getInputStream();
        RecognizeIdcardRequest recognizeIdcardRequest = RecognizeIdcardRequest.builder()
                                                                              .body(bodyStream)
                                                                              // Request-level configuration rewrite, can set Http request parameters, etc.
                                                                              // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                                                                              .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<RecognizeIdcardResponse> response = client.recognizeIdcard(recognizeIdcardRequest);
        // Synchronously get the return value of the API request
        RecognizeIdcardResponse resp = response.get();
        client.close();
        return JSON.toJSONString(resp);
    }
}
